<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * CMS
 */
namespace Pay\Controller;
use Think\Controller;

/**
 * 系统设置
 */
class OrdersController extends Controller{
	
	/**
	 * 创建订单
	 */
    public function Create(){
		
		$m_pay_order = M('pay_order');
		
		$map['today'] = getToday();
		$num = $m_pay_order->where($map)->count();
		
		$data['order_id'] = date('ymd',time()).sprintf("%06d",$num+1);
		$data['name'] = I('param.name');
		$data['money'] = I('param.money');
		$data['time'] = time();
		$data['ip'] = get_client_ip();
		$data['today'] = getToday();
		$data['status'] = 2;
		
		//记录订单
		setPayOrderLog($data['order_id'],'创建订单');
		
	
		$m_pay_order->add($data);
		
		$order_id = $data['order_id'];
		
		
		
		if($this->isMobile()){
			
			redirect("/Pay/Orders/wap/id/$order_id", 0, '页面跳转中...');
		
		}else{
			
			redirect("/Pay/Orders/pc/id/$order_id", 0, '页面跳转中...');
		
		}
		
		
		
	}
	
	public function isMobile()
 { 
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
    {
        return true;
    } 
    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER['HTTP_VIA']))
    { 
        // 找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    } 
    // 脑残法，判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER['HTTP_USER_AGENT']))
    {
        $clientkeywords = array ('nokia',
            'sony',
            'ericsson',
            'mot',
            'samsung',
            'htc',
            'sgh',
            'lg',
            'sharp',
            'sie-',
            'philips',
            'panasonic',
            'alcatel',
            'lenovo',
            'iphone',
            'ipod',
            'blackberry',
            'meizu',
            'android',
            'netfront',
            'symbian',
            'ucweb',
            'windowsce',
            'palm',
            'operamini',
            'operamobi',
            'openwave',
            'nexusone',
            'cldc',
            'midp',
            'wap',
            'mobile'
            ); 
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
        {
            return true;
        } 
    } 
    // 协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT']))
    { 
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
        {
            return true;
        } 
    } 
    return false;
 } 

	/**
	 * PC选择
	 */
    public function PC(){
		
		
		$m_pay_order = M('pay_order');
		
		$map['order_id'] = I('param.id');
		$map['status'] = 2;
		
		$data = $m_pay_order->where($map)->find();
		
		if(!$data){
			
			$this->error('订单已经支付，请勿重复支付。');
			die;
		}
		
		$this->assign('data',$data);
		
		
		$base_url = $_SERVER['SERVER_NAME'];
		
		$this->assign('base_url',$base_url);
		
		$this->display();
	}
	
	/**
	 * 手机选择
	 */
    public function Wap(){
		
		$m_pay_order = M('pay_order');
		
		$map['order_id'] = I('param.id');
		$map['status'] = 2;
		$data = $m_pay_order->where($map)->find();
		
		if(!$data){
			
			$this->error('订单已经支付，请勿重复支付。');
			die;
		}
		
		$this->assign('data',$data);
		
		$base_url = $_SERVER['SERVER_NAME'];
		
		$this->assign('base_url',$base_url);
		
		$this->display();
	}
	
	
	//微信支付开始
	
	/**
	 * 微信扫码支付
	 */
    public function WeiXinPay(){
		
		
		$id = I('param.id');
		
		$m_pay_order = M('pay_order');
		
		
		$map['order_id'] = $id;
		$data = $m_pay_order->where($map)->find();
		
		
		$base_url = $_SERVER['SERVER_NAME'];
		$url = 'http://'.$base_url.'/pay/weixinpay/pc/example/native.php?id='.$id;
		
		$url = file_get_contents($url);
		
		$this->assign('url',$url);
		$this->assign('data',$data);
		$base_url = $_SERVER['SERVER_NAME'];
		
		$this->assign('id',$map['id']);
		$this->assign('base_url',$base_url);
		
		$this->display();
		
	}
	
	public function WeiXinPayOK(){
		
		
		$id = I('param.id');
		
		$m_pay_order = M('pay_order');
		
		
		$map['order_id'] = $id;
		$map['status'] = 1;
		$data = $m_pay_order->where($map)->find();
		
		if($data){
			
			echo 1;
			
		}
	}
	
	
	//支付宝支付
	/**
	 * 创建订单
	 */
    public function AliPay(){
		
		
		$id = I('param.id');
		
		$m_pay_order = M('pay_order');
		
		
		$map['order_id'] = $id;
		$data = $m_pay_order->where($map)->find();
		
		//$data['money'] = 0.01;
		
		$base_url = $_SERVER['SERVER_NAME'];
		$url = 'http://'.$base_url.'/pay/alipay/pc/alipayapi.php?WIDout_trade_no='.$id.'&WIDsubject='.$data['name'].'&WIDtotal_fee='.$data['money'].'&WIDbody='.$data['name'];
		
		redirect($url, 0, '页面跳转中...');
		
	}
	
	 public function WapAliPay(){
		
		
		$id = I('param.id');
		
		$m_pay_order = M('pay_order');
		
		
		$map['order_id'] = $id;
		$data = $m_pay_order->where($map)->find();
		
		//$data['money'] = 0.01;
		
		$base_url = $_SERVER['SERVER_NAME'];
		$url = 'http://'.$base_url.'/pay/alipay/wap/alipayapi.php?WIDout_trade_no='.$id.'&WIDsubject='.$data['name'].'&WIDtotal_fee='.$data['money'].'&WIDbody='.$data['name'];
		
		redirect($url, 0, '页面跳转中...');
		
	}
	
}